package com.aaa.mythread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread7scaleticket extends Thread{
    private int ticket=10;
    @Override
    public  void run() {
        //  业务
        Lock lock = new ReentrantLock(true); // 加公平锁

        while (true) {
            lock.lock();// 手动加锁
            try {
                selltic();
            }catch (Exception e){
                e.printStackTrace();

            }finally {
                lock.unlock();// 释放锁

            }
        }
    }

    // java
    // 同步方法
    public  void selltic(){
       // while (true) {
            // synchronized （锁对象）{}
            if (ticket > 0) {
                //  1
                // 1
                System.out.println(Thread.currentThread().getName() + "卖出了一张票，还有" + ticket--);
            }
        //}
    }



    public static void main(String[] args) {
        MyThread7scaleticket myThread4scaleticket = new MyThread7scaleticket();
        Thread thread = new Thread(myThread4scaleticket,"zs");// 4  2
        Thread thread2 = new Thread(myThread4scaleticket,"ls");// 3 3
        Thread thread3 = new Thread(myThread4scaleticket,"ww");// 3 3
        Thread thread4 = new Thread(myThread4scaleticket,"ww1");// 3 3


        thread.start();
        thread2.start();
        thread3.start();
        thread4.start();


    }
}
